package com.letoken.platform.admin.controller;

import com.letoken.platform.pub.annotation.auth.Permission;
import com.letoken.platform.pub.res.ApiResponse;
import com.letoken.platform.repo.app.dao.VideoRedEnvelopesConfigMapper;
import com.letoken.platform.repo.app.po.VideoRedEnvelopesConfig;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/video/red_envelope/config")
public class VideoRedEnvelopesConfigController {

    @Resource
    private VideoRedEnvelopesConfigMapper mapper;


    @Permission(uri = "/video/red_envelope/config/list", qx = "/video/red_envelope/config/list")
    @GetMapping("/list")
    public ApiResponse list() {
        List<VideoRedEnvelopesConfig> configs = mapper.selectList(null);
        Map<String, Object> result = new HashMap<>();
        for (VideoRedEnvelopesConfig config : configs) {
            if (config.effectiveStartTime.isBefore(LocalDateTime.now()) && config.effectiveEndTime.isAfter(LocalDateTime.now())) {
                result.put("active", config);
            }
        }
        result.put("list", configs);
        return ApiResponse.create(result);
    }

    @Permission(uri = "/video/red_envelope/config/update", qx = "/video/red_envelope/config/update")
    @PostMapping("/update")
    public ApiResponse update(@RequestBody VideoRedEnvelopesConfig req) {
        VideoRedEnvelopesConfig videoRedEnvelopesConfig = new VideoRedEnvelopesConfig();
        videoRedEnvelopesConfig.id = req.id;
        videoRedEnvelopesConfig.maximumQuantityPerDay = req.maximumQuantityPerDay;
        videoRedEnvelopesConfig.minimumAmount = req.minimumAmount;
        videoRedEnvelopesConfig.maximumAmount = req.maximumAmount;
        videoRedEnvelopesConfig.capPercentage = req.capPercentage;
        videoRedEnvelopesConfig.maximumTotalAmount = req.maximumTotalAmount;
        videoRedEnvelopesConfig.effectiveStartTime = req.effectiveStartTime;
        videoRedEnvelopesConfig.effectiveEndTime = req.effectiveEndTime;

        List<VideoRedEnvelopesConfig> configs = mapper.selectTimeConflictAndFilterId(req.id, req.effectiveStartTime, req.effectiveEndTime);
        if(!CollectionUtils.isEmpty(configs)){
            return ApiResponse.create(862225522,"有效期冲突");
        }
        mapper.updateById(videoRedEnvelopesConfig);
        return ApiResponse.create();
    }


    @Permission(uri = "/video/red_envelope/config/add", qx = "/video/red_envelope/config/add")
    @PostMapping("/add")
    public ApiResponse add(@RequestBody VideoRedEnvelopesConfig req) {
        VideoRedEnvelopesConfig videoRedEnvelopesConfig = new VideoRedEnvelopesConfig();
        videoRedEnvelopesConfig.id = req.id;
        videoRedEnvelopesConfig.maximumQuantityPerDay = req.maximumQuantityPerDay;
        videoRedEnvelopesConfig.minimumAmount = req.minimumAmount;
        videoRedEnvelopesConfig.maximumAmount = req.maximumAmount;
        videoRedEnvelopesConfig.capPercentage = req.capPercentage;
        videoRedEnvelopesConfig.maximumTotalAmount = req.maximumTotalAmount;
        videoRedEnvelopesConfig.effectiveStartTime = req.effectiveStartTime;
        videoRedEnvelopesConfig.effectiveEndTime = req.effectiveEndTime;

        List<VideoRedEnvelopesConfig> configs = mapper.selectTimeConflict(req.effectiveStartTime, req.effectiveEndTime);
        if(!CollectionUtils.isEmpty(configs)){
            return ApiResponse.create(862225522,"有效期冲突");
        }

        mapper.insert(videoRedEnvelopesConfig);
        return ApiResponse.create();
    }
}
